package com.apartment.control;

import com.apartment.data.entity.Apartment;
import com.apartment.data.entity.Collect;
import com.apartment.data.entity.User;
import com.apartment.sevice.ServeService;
import com.apartment.sevice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * Author : xuzhiyong
 * Date : 2022/6/5 22:22
 */
@Controller
@RequestMapping("/serve")
public class ServeController {
    @Autowired
    private ServeService serveService;

    @Autowired
    private UserService userService;

    @RequestMapping("/collect")
    public String collect(Collect collect,Model model,Integer user_id, Integer apartment_id){
        collect.getUser_id(user_id);
        collect.getApartment_id(apartment_id);
        serveService.addCollect(collect);
        System.out.println(collect);
        User user =userService.findUserById(user_id);
        model.addAttribute("user",user);
        return "user/userCollectSuccess";
    }

    @RequestMapping("/delete")
    public String delete(Model model,Integer cid,Integer uid){
        User user =userService.findUserById(uid);
        model.addAttribute("user",user);
        serveService.deleteCollectById(cid);
        return "user/userCancleCollectSuccess";
    }

    @RequestMapping("/select")
    public String select(Model model,Integer id,@RequestParam("keyword") String keyword){
        User user =userService.findUserById(id);
        model.addAttribute("user",user);
        List<Map<String, Object>> apartments = serveService.findApartmentByLike(keyword);
        model.addAttribute("apartments",apartments);
        return "apartment/apartmentListForUser";
    }


}
